package org.imlinker.plugin.say;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import org.imlinker.plugin.Say;
import org.imlinker.util.StringUtils;

@SuppressWarnings("serial")
public class SayWindow extends JFrame {

	public SayWindow(Say say) {
		this.say = say;

		initComponents();
		initActions();

		setTitle("Say");
		setLocationRelativeTo(null);
	}

	public void display(final String message) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				displayArea.append(message + "\n");
				displayArea.setCaretPosition(displayArea.getText().length());
			}
		});

	}

	public void initActions() {
		inputArea.addKeyListener(new KeyAdapter() {

			@Override
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() != '\n') {
					return;
				}

				String m = inputArea.getText().trim();
				say.send(m);
				m = "Me:" + m;
				display(m);
				inputArea.setText("");
			}

		});

		followMenu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new FollowDialog(SayWindow.this, true, say).setVisible(true);
			}

		});

		editMenu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String user = say.getUser();
				String pass = say.getPassword();

				JPanel panel = new JPanel();
				panel.setLayout(new GridLayout(3, 2));
				panel.add(new JLabel("UserName:"));
				JTextField userField = new JTextField(15);
				userField.setText(user);
				panel.add(userField);
				panel.add(new JLabel("Password:"));
				JPasswordField passField = new JPasswordField(15);
				passField.setText(pass);
				panel.add(passField);
				panel.add(new JLabel(""));
				JCheckBox box = new JCheckBox("Save password?", false);
				panel.add(box);
				int result = JOptionPane.showConfirmDialog(null, panel,
						"Input account", JOptionPane.OK_CANCEL_OPTION);
				if (result == JOptionPane.CANCEL_OPTION) {
					return;
				}

				String username = userField.getText();
				String password = new String(passField.getPassword());
				if (StringUtils.isNotEmpty(username)
						&& StringUtils.isNotEmpty(password)) {
					say.setUser(username);
					say.setPassword(password);
				}
			}

		});
	}

	private void initComponents() {

		JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
		displayArea = new javax.swing.JTextArea();
		JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
		inputArea = new javax.swing.JTextArea();
		JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
		JMenu jMenu1 = new javax.swing.JMenu();
		followMenu = new javax.swing.JMenuItem();
		editMenu = new javax.swing.JMenuItem();

		displayArea.setColumns(20);
		displayArea.setEditable(false);
		displayArea.setLineWrap(true);
		displayArea.setRows(5);
		displayArea.setWrapStyleWord(true);
		jScrollPane1.setViewportView(displayArea);

		inputArea.setColumns(20);
		inputArea.setLineWrap(true);
		inputArea.setRows(5);
		inputArea.setWrapStyleWord(true);
		jScrollPane2.setViewportView(inputArea);

		jMenu1.setText("Setting");

		followMenu.setText("Follow");
		jMenu1.add(followMenu);

		editMenu.setText("Account");
		jMenu1.add(editMenu);

		jMenuBar1.add(jMenu1);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 416,
				Short.MAX_VALUE).addComponent(jScrollPane2,
				javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addComponent(jScrollPane1,
						javax.swing.GroupLayout.DEFAULT_SIZE, 372,
						Short.MAX_VALUE).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollPane2,
								javax.swing.GroupLayout.PREFERRED_SIZE, 96,
								javax.swing.GroupLayout.PREFERRED_SIZE)));

		pack();
	}// </editor-fold>

	private JTextArea displayArea;

	private JTextArea inputArea;

	private JMenuItem followMenu;

	private JMenuItem editMenu;

	private Say say;

}
